// Angular Version 6 below need to be deleted {providedIn: 'root'}

// angular
import {Injectable} from '@angular/core';
import {HttpParams} from '@angular/common/http';
// owner
import {Request} from 'pengesoft-ng-lib';
// models
import { RepairApplyList } from '../domains/repaireApplyList.domain';
import {RepairApply} from '../domains/repairapply.domain';

/**
 *
 */
@Injectable({
  providedIn: 'root'
})
export class RepairApplyServiceSvr {
  private readonly baseUrl: string;

  constructor(
    private request: Request
  ) {
    this.baseUrl = 'repairApplyServiceSvr.assx/';
  }


  updateRepairApplyState(token: string, id: string, status: number): Promise<number> {
    const httpParams = new HttpParams()
      .append('token', token.toString())
      .append('id', id.toString())
      .append('status', status.toString());
    return this.request.post<number>(this.baseUrl + 'updateRepairApplyState', httpParams).then((res) => {
      return res;
    });
  }


  listRepairApplications(token: string, state: number, isWithdraw: number, startTime: string, endTime: string, skip: number, size: number): Promise<RepairApplyList> {
    const httpParams = new HttpParams()
      .append('token', token.toString())
      .append('state', state.toString())
      .append('isWithdraw', isWithdraw.toString())
      .append('startTime', startTime.toString())
      .append('endTime', endTime.toString())
      .append('skip', skip.toString())
      .append('size', size.toString());
    return this.request.post<RepairApplyList>(this.baseUrl + 'listRepairApplications', httpParams).then((res) => {
      return res;
    });
  }


  updateIsWithdraw(token: string, id: string): Promise<number> {
    const httpParams = new HttpParams()
      .append('token', token.toString())
      .append('id', id.toString());
    return this.request.post<number>(this.baseUrl + 'updateIsWithdraw', httpParams).then((res) => {
      return res;
    });
  }


  saveRepairApply(token: string, deviceId: string, type: number, applicantId: string, description: string): Promise<RepairApply> {
    const httpParams = new HttpParams()
      .append('token', token.toString())
      .append('deviceId', deviceId.toString())
      .append('type', type.toString())
      .append('applicantId', applicantId.toString())
      .append('description', description.toString());
    return this.request.post<RepairApply>(this.baseUrl + 'saveRepairApply', httpParams).then((res) => {
      return new RepairApply(res);
    });
  }


  GetVersion(): Promise<string> {
    const httpParams = new HttpParams();
    return this.request.post<string>(this.baseUrl + 'GetVersion', httpParams).then((res) => {
      return res;
    });
  }


  getRepairApplyByPrimaryKey(token: string, id: string): Promise<RepairApply> {
    const httpParams = new HttpParams()
      .append('token', token.toString())
      .append('id', id.toString());
    return this.request.post<RepairApply>(this.baseUrl + 'getRepairApplyByPrimaryKey', httpParams).then((res) => {
      return new RepairApply(res);
    });
  }


  updateRepairApply(token: string, id: string, description: string): Promise<number> {
    const httpParams = new HttpParams()
      .append('token', token.toString())
      .append('id', id.toString())
      .append('description', description.toString());
    return this.request.post<number>(this.baseUrl + 'updateRepairApply', httpParams).then((res) => {
      return res;
    });
  }
}
